package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.ShopMapper;
import com.gxa.pingan.pojo.City;
import com.gxa.pingan.pojo.Shop;
import com.gxa.pingan.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class ShopServiceImpl implements ShopService {
    @Autowired
    ShopMapper shopMapper;
    @Override
    public List<Map> findAll(Integer storesType) {
        return shopMapper.findAll(storesType);
    }

    @Override
    public List<Map> selectFindByPage(Integer offset, Integer size,Integer storesType) {
        List<Map> list = shopMapper.selectFindByPage((offset - 1) * size, size, storesType);
        for (int i = 0; i <list.size() ; i++) {
           // (Integer)list.get(i).get("discounts")*10;
           // Double.valueOf(map.get("discounts").toString())*10
            //list.get(i).get("discounts")
            list.get(i).put("discounts",Double.valueOf(list.get(i).get("discounts").toString())/10);
        }

        return list;
    }

    @Override
    public void delete(Long id) {
        shopMapper.delete(id);
    }

    @Override
    public void add(Map map) throws BusinessException {

        String name =  map.get("name").toString();

        List<Shop> list = shopMapper.findByName(name);
        if (list.size()!=0){
            throw new BusinessException(ExceptionCode.REPEAT_SHOP);
        }
        //Integer i =10;
     //  map.put("discounts",(Double)map.get("discounts")*10);
        //因为这里前端可能传Integer类型的数据，所以要先把它转换为字符串，在用Double.valueof方法
        map.put("discounts",Double.valueOf(map.get("discounts").toString())*10) ;

        shopMapper.add(map);
    }

    @Override
    public void update(Long id, Shop shop) throws BusinessException {
        Shop byId = shopMapper.findById(id);

        shop.setDiscounts(shop.getDiscounts()*10);
        if (byId !=null){
            shopMapper.update(shop);
        }

        List<Shop> list = shopMapper.findByName(shop.getName());
        if (list.size()>=2){
            throw new BusinessException(ExceptionCode.EXIT_SHOP);
        }

    }

    @Override
    public List<Map> findLikeByName(String name, Integer type) {
        return shopMapper.findLikeByName(name,type);
    }

    @Override
    public List<Map> findLikeByNameAndPage(String name, Integer type, Integer offset, Integer size) {
        return shopMapper.findLikeByNameAndPage(name,type,(offset-1)*size,size);
    }

    @Override
    public List<Shop> findLikeByNameAndPageOrFindAll(Integer type, String name) {

        List<Shop> likeByNameAndPageOrFindAll = shopMapper.findLikeByNameAndPageOrFindAll(type, name);

        return likeByNameAndPageOrFindAll;
    }

    @Override
    public Shop findById(Long id) {
        return shopMapper.findById(id);
    }
}
